from PyQt5.QtCore import QCoreApplication,QUrl,QObject,QTimer
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQuick import QQuickView
import random

class Welcome(QQuickView):
    def __init__(self):
        super().__init__()

        self.setSource(QUrl("../resource/qml/welcome.qml"))
        self.root_obj = self.rootObject()
        self.text_label = self.root_obj.findChild(QObject, "textLabel")
        self._count = 0

        print('init success')
    def auto_test(self):
        self.timer = QTimer()
        self.timer.timeout.connect(self.slot_timer_timeout)
        self.timer.setInterval(700)
        self.timer.setSingleShot(False)
        self.timer.start()


    def slot_timer_timeout(self):
        dat = 'I am %d'%(random.randrange(1000, 5000))

        self.add_new_message(dat)


    def add_new_message(self, line):
        text = self.text_label.property('text')
        text = text + '\n' + line
        self.text_label.setProperty('text', text)
        self._count =  self._count + 1
        if self._count > 10 :
            self.clear_screen()
        pass
    def clear_screen(self):
        self._count = 0
        self.text_label.setProperty('text', '\n'*4)




def add_new_line_to_welcome(obj):
    obj.slot_timer_timeout()





if __name__ == '__main__' :
    app = QGuiApplication([])
    info =  Welcome()
    info.show()
    info.auto_test()


    app.exec_()